var map;
var poi;

$(function(){

	$("#dg").datagrid({
		onSelect:function(rowIndex,rowData){
			addUser(rowData);
		},
		onUnselect:function(rowIndex,rowData){
			var name = rowData.name;
			removeMarker(name);
			//alert(markerMap.length);
			//removeUser(new BMap.Point(121.553252, 31.245336), name);
		}
	});


	// 百度地图API功能
	map = new BMap.Map('allmap');
	poi = new BMap.Point(121.537729, 31.234375);
	map.centerAndZoom(poi, 15);
	map.enableScrollWheelZoom();
	map.addControl(navigationControl);

	//assign task
	$("#assignTaskBtn").click(function(){
		assignTask();
	});
});

//assign task to user
function assignTask(){
	var rows = $('#dg').datagrid('getChecked');
	if(rows.length==0){
		messager_show("请选择外勤人员",3000);
		return;
	}
	var _userIdArr = '';
	for(var i=0; i<rows.length; i++){
		var row = rows[i];
		var userId = row.id;
		_userIdArr = _userIdArr+userId+",";
	}
	//_taskId

	// ajax
	var _url = "/springmvc/task/assignTask.mvc";
	_url = _path + _url;
	var _param = {};
	_param.taskId = _taskId;
	_param.userIdArr = _userIdArr;

	$.post(_url,_param,function(data){
		if(data.success!=true){
			messager_show(data.content,3000);
			return;
		}
		messager_show("任务指派成功",3000);

	});



}

//backup market infomation for remove function
var markerJA = [];

function setTrackListByUser(userId,marker){

	// ajax
	var _url = "/springmvc/task/queryUserTrackList.mvc";
	_url = _path + _url;
	var _param = {};
	_param.a2userId = userId;

	var pointList = [];
	$.post(_url,_param,function(data){
		if(data.success!=true){
			messager_show(data.content,3000);
			return;
		}
		$.each(data.resultMap.rows,function(i,val){
			var point = new BMap.Point(val.lng, val.lat);
			pointList.push(point);
		});
		/*
		 * 设置新的标注位置
		 */
		marker.setPosition(pointList[pointList.length-1]);
		map.centerAndZoom(pointList[pointList.length-1], 15);

		var polyline = new BMap.Polyline(pointList, {
			strokeColor: "#DC1118",
			strokeWeight: 3,
			strokeOpacity: 0.7
		});
		/*
		 * 修改移动轨迹
		 */
		map.addOverlay(polyline);
	});



}

function addUser(rowData) {
	var name = rowData.name;
	var userId = rowData.id;

	var point = new BMap.Point(121.553252, 31.245336);

	var myIcon = new BMap.Icon("face.jpg", new BMap.Size(36, 36), {
		anchor: new BMap.Size(18, 57)
	});
	myIcon.setImageSize(new BMap.Size(36, 36));

	//创建marker对象
	var marker = new BMap.Marker(point, {
		icon: myIcon
	});
	var shadowIcon = new BMap.Icon("face_bg.png", new BMap.Size(50, 64), {
		anchor: new BMap.Size(25, 64)
	});
	shadowIcon.setImageSize(new BMap.Size(50, 64));

	marker.setShadow(shadowIcon);
	var label = new BMap.Label(name, {
		offset: new BMap.Size(0, 60)
	});
	marker.setLabel(label);
	label.setStyle({
		color: "#467711",
		fontSize: "14px",
		fontFamily: "微软雅黑",
		border: "none",
		borderRadius: "2px",
		padding: "0px 5px",
		backgroundColor: "rgba(0,0,0,0)",
		fontWeight: "bold"
	});

	marker.setTitle(name);
	map.addOverlay(marker);
	marker.setAnimation(BMAP_ANIMATION_DROP);

	//地图上加标点和路径
	setTrackListByUser(userId,marker);


	var markerJO = {};
	markerJO.name=name;
	markerJO.marker=marker;
	markerJA.push(markerJO);
}

//???????
function removeMarker(name){
	alert(44);
	$.each(markerJA,function(i,markerJO){
		if(name == markerJO.name){
			//markerJA.splice(i, 1);
			map.removeOverlay(markerJO.marker)
		}
		//alert(markerJO.name)
	})
}


// 添加缩放控件
var navigationControl = new BMap.NavigationControl({
	// 靠左上角位置
	anchor: BMAP_ANCHOR_TOP_LEFT,
	// LARGE类型
	type: BMAP_NAVIGATION_CONTROL_LARGE,
	// 启用显示定位
	enableGeolocation: true
});
